package cn.iocoder.yudao.module.bpm.controller.admin.taginfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bpm.controller.admin.taginfo.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.taginfo.BpmTagInfoDO;
import cn.iocoder.yudao.module.bpm.service.taginfo.BpmTagInfoService;

@Tag(name = "管理后台 - 标签信息")
@RestController
@RequestMapping("/bpm/tag-info")
@Validated
public class BpmTagInfoController {

    @Resource
    private BpmTagInfoService tagInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建标签信息")
    @PreAuthorize("@ss.hasPermission('bpm:tag-info:create')")
    public CommonResult<Long> createTagInfo(@Valid @RequestBody BpmTagInfoSaveReqVO createReqVO) {
        return success(tagInfoService.createTagInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签信息")
    @PreAuthorize("@ss.hasPermission('bpm:tag-info:update')")
    public CommonResult<Boolean> updateTagInfo(@Valid @RequestBody BpmTagInfoSaveReqVO updateReqVO) {
        tagInfoService.updateTagInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标签信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:tag-info:delete')")
    public CommonResult<Boolean> deleteTagInfo(@RequestParam("id") Long id) {
        tagInfoService.deleteTagInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标签信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:tag-info:query')")
    public CommonResult<BpmTagInfoRespVO> getTagInfo(@RequestParam("id") Long id) {
        BpmTagInfoDO tagInfo = tagInfoService.getTagInfo(id);
        return success(BeanUtils.toBean(tagInfo, BpmTagInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得标签信息分页")
    @PreAuthorize("@ss.hasPermission('bpm:tag-info:query')")
    public CommonResult<PageResult<BpmTagInfoRespVO>> getTagInfoPage(@Valid BpmTagInfoPageReqVO pageReqVO) {
        PageResult<BpmTagInfoDO> pageResult = tagInfoService.getTagInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BpmTagInfoRespVO.class));
    }

    @GetMapping("/select")
    @Operation(summary = "获取指定标签组的标签列表")
    public CommonResult<List<BpmTagInfoRespVO>> getTagInfoByGroupId(@RequestParam("groupId") Long groupId){
        List<BpmTagInfoDO> list = tagInfoService.getTagInfoByGroupId(groupId);
        return success(BeanUtils.toBean(list,BpmTagInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标签信息 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:tag-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTagInfoExcel(@Valid BpmTagInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BpmTagInfoDO> list = tagInfoService.getTagInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "标签信息.xls", "数据", BpmTagInfoRespVO.class,
                        BeanUtils.toBean(list, BpmTagInfoRespVO.class));
    }

}